import { login, logout, refreshToken } from '@/api/auth'
import { getInfo } from '@/api/account'
import {
  getToken,
  setToken,
  removeToken,
  getRefreshToken,
  setRefreshToken,
  removeRefreshToken
} from '@/utils/auth'
import router, { resetRouter } from '@/router'

// 状态定义
const state = {
  token: getToken(),
  refreshToken: getRefreshToken(), // 刷新令牌，用于获取新的访问令牌
  name: '',
  avatar: '',
  introduction: '',
  roles: []
}

// 状态变更
const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_REFRESH_TOKEN: (state, refreshToken) => {
    state.refreshToken = refreshToken
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  }
}

// 异步操作
const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password, codeKey, code } = userInfo
    return new Promise((resolve, reject) => {
      login({
        username: username.trim(),
        password: password,
        codeKey: codeKey,
        code: code
      })
        .then((response) => {
          const { token, refreshToken } = response
          commit('SET_TOKEN', token)
          setToken(token)
          commit('SET_REFRESH_TOKEN', refreshToken)
          setRefreshToken(refreshToken)
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token)
        .then((response) => {
          const { roles, name, avatar, introduction } = response

          if (!roles || roles.length <= 0) {
            reject('getInfo: roles must be a non-null array!')
          }

          commit('SET_ROLES', roles)
          commit('SET_NAME', name)
          commit(
            'SET_AVATAR',
            avatar ||
              'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'
          )
          commit('SET_INTRODUCTION', introduction)
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // user logout
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      logout(state.token)
        .then(() => {
          commit('SET_TOKEN', '')
          commit('SET_REFRESH_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          removeRefreshToken()
          resetRouter()

          // reset visited views and cached views
          // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
          dispatch('tagsView/delAllViews', null, { root: true })

          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      commit('SET_TOKEN', '')
      commit('SET_REFRESH_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      removeRefreshToken()
      resolve()
    })
  },

  // 刷新Token
  // 1.临时使用 refreshToken 作为访问令牌
  // 2.调用刷新令牌 API
  // 3.获取新的访问令牌和刷新令牌
  // 4.更新状态和本地存储

  refreshToken({ commit }) {
    // 普通API请求使用：Bearer <AccessToken>， RefreshToken是存储在cookie，不用于普通请求  临时使用 refreshToken 作为访问令牌
    setToken(getRefreshToken())
    commit('SET_TOKEN', getRefreshToken())
    return new Promise((resolve, reject) => {
      refreshToken()
        .then((response) => {
          // 更新新的访问令牌和刷新令牌
          const { token, refreshToken } = response
          commit('SET_TOKEN', token)
          setToken(token)
          commit('SET_REFRESH_TOKEN', refreshToken)
          setRefreshToken(refreshToken)
          resolve()
        })
        .catch((error) => {
          // refreshToken失败时，清除所有token信息
          console.log('RefreshToken失败:', error)
          commit('SET_TOKEN', '')
          commit('SET_REFRESH_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          removeRefreshToken()
          reject(error)
        })
    })
  },

  // dynamically modify permissions
  async changeRoles({ commit, dispatch }, role) {
    const token = role + '-token'

    commit('SET_TOKEN', token)
    setToken(token)

    const { roles } = await dispatch('getInfo')

    resetRouter()

    // generate accessible routes map based on roles
    const accessRoutes = await dispatch('permission/generateRoutes', roles, {
      root: true
    })
    // dynamically add accessible routes
    router.addRoutes(accessRoutes)

    // reset visited views and cached views
    dispatch('tagsView/delAllViews', null, { root: true })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
